import pytest
import pymysql
import requests

@pytest.fixture(scope='module')
def database_connect():
    connection = pymysql.connect(
        host='localhost',
        user='root',
        password='cpy50686868cpy',
        database='Prac'
    )
    c = connection.cursor()
    yield c
    c.close()
    connection.close()


def test_all(database_connect):
    #第一步：先使用登录接口 获取到jwt
    #模拟发送
    res = requests.post(
        'http://127.0.0.1:8082/post',
        params={'user':123},
        headers={'athuor':'caipeiyan'},
        json={'des':'这个一个post请求','jwt':'2fjldsaj2'}
        )
    #模拟获取到登录的返回，并提取jwt
    res_json = res.json()
    jwt = res_json['json']['jwt']

    #用获取到的jwt办理业务

    res = requests.get(
        'http://127.0.0.1:8082/bearer',
        headers={'Authorization':f'Bearer {jwt}'},
        params={'des':'这是一个业务接口,携带了jwt进行认证'}
        )
    print(res.status_code)
    print(res.json())
    assert res.status_code == 200

    #模拟读取数据库中的数据
    rows = database_connect.execute('select * from user')
    print(database_connect.fetchall())
    print(rows)
    assert rows != 0